package com.ilvyou.api.controller.finance;

import com.ilvyou.api.common.Cache;
import com.ilvyou.api.controller.BaseController;
import com.ilvyou.api.bean.result.MainInfo;
import com.ilvyou.api.bean.result.Result;
import com.ilvyou.api.service.BankService;
import com.ilvyou.api.service.FinanceService;
import com.ilvyou.api.service.PersonalService;
import com.ilvyou.api.service.UserService;
import com.ilvyou.api.util.RemoteUtil;
import com.ilvyou.core.util.StringUtil;
import com.ilvyou.data.lhf.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by GuanYuCai on 16/9/17.
 */
@Controller
public class FinanceController extends BaseController {

    @Autowired
    private FinanceService financeService;
    @Autowired
    private BankService bankService;
    @Autowired
    private UserService userService;
    @Autowired
    private PersonalService personalService;

    @RequestMapping("/finance/baseInfo")
    @ResponseBody
    public Result info(@RequestParam String token) throws Exception{
        MainInfo rs = financeService.getMainInfo(getUserId(token));
        return success(rs);
    }

    @RequestMapping(value = "/finance/withdraw", method = RequestMethod.POST)
    @ResponseBody
    public Result withdraw(@RequestParam String token, @RequestParam String card, @RequestParam double amount,
                           @RequestParam String paypassword) throws Exception {
        Cache.User user = Cache.get(token);
        Long actId = user.actId;
        AlipayPeriodEntity period = financeService.getAlipayPeriod(actId);
        if (period == null){
            return fail("该用户不能进行金额操作");
        }

        ScrAlipayEntity alipay = userService.getByPK(ScrAlipayEntity.class, actId);
        SrcAuthbaseEntity srcAuthBase = personalService.getAuthbaseByActid(actId);
        if (StringUtil.isEmpty(alipay.getBpploginname()) || srcAuthBase == null){
            return fail("请先前往电脑端补全信息，再进行操作。");
        }

        AlipayPbankEntity pbank = bankService.getByPK(AlipayPbankEntity.class, card);
        if (pbank == null){
            return fail("您不存在此银行卡，请先添加银行卡");
        }else if(("Credit").equals(pbank.getBanktype())){
            return fail("信用卡不支持提现");
        }

        AlipayAccountEntity account = userService.getByPK(AlipayAccountEntity.class, actId);
        paypassword = StringUtil.base64Decode(paypassword);
        paypassword = RemoteUtil.getMd5Pwd(paypassword, alipay.getSalt());
        if (!paypassword.equals(account.getPaypassword())){
            return fail("交易密码不正确");
        }

        if (amount < account.getDrawallimit()){
            return fail("对不起，您的提现金额不能小于提现金额" + StringUtil.toCurrency(account.getDrawallimit()));
        }else if (amount > account.getRightamount()){
            return fail("本次最多可提现" + StringUtil.toCurrency(account.getRightamount()) + "元");
        }

        financeService.addWithdraw(alipay, account, period, pbank, amount, user.source);

        return success("");
    }
}
